<?php

header('Content-type:text/html;charset=utf-8');
include_once __DIR__ . '/SDKConfig.php';

/**
 * 数组 排序后转化为字体串
 *
 * @param array $params        	
 * @return string
 */
function coverParamsToString($params) {
    $sign_str = '';
    // 排序
    ksort($params);
    foreach ($params as $key => $val) {
        if ($key == 'signature') {
            continue;
        }
        $sign_str .= sprintf("%s=%s&", $key, $val);
        // $sign_str .= $key . '=' . $val . '&';
    }
    return substr($sign_str, 0, strlen($sign_str) - 1);
}

/**
 * 字符串转换为 数组
 *
 * @param unknown_type $str        	
 * @return multitype:unknown
 */
function coverStringToArray($str) {
    $result = array();

    if (!empty($str)) {
        $temp = preg_split('/&/', $str);
        if (!empty($temp)) {
            foreach ($temp as $key => $val) {
                $arr = preg_split('/=/', $val, 2);
                if (!empty($arr)) {
                    $k = $arr ['0'];
                    $v = $arr ['1'];
                    $result [$k] = $v;
                }
            }
        }
    }
    return $result;
}

/**
 * 处理返回报文 解码客户信息 , 如果编码为utf-8 则转为utf-8
 *
 * @param unknown_type $params        	
 */
function deal_params(&$params) {
    /**
     * 解码 customerInfo
     */
    if (!empty($params ['customerInfo'])) {
        $params ['customerInfo'] = base64_decode($params ['customerInfo']);
    }

    if (!empty($params ['encoding']) && strtoupper($params ['encoding']) == 'utf-8') {
        foreach ($params as $key => $val) {
            $params [$key] = iconv('utf-8', 'UTF-8', $val);
        }
    }
}

/**
 * 压缩文件 对应java deflate
 *
 * @param unknown_type $params        	
 */
function deflate_file(&$params) {
    foreach ($_FILES as $file) {

        $params ['fileName'] = $file ['name'];

        $file_content = file_get_contents($file ['tmp_name']);
        $file_content_deflate = gzcompress($file_content);

        $params ['fileContent'] = base64_encode($file_content_deflate);
    }
}

/**
 * 处理报文中的文件
 *
 * @param unknown_type $params        	
 */
function deal_file($params) {
    if (isset($params ['fileContent'])) {
        $fileContent = $params ['fileContent'];

        if (empty($fileContent)) {
        } else {
            // 文件内容 解压缩
            $content = gzuncompress(base64_decode($fileContent));
            $root = SDK_FILE_DOWN_PATH;
            $filePath = null;
            if (empty($params ['fileName'])) {
                $filePath = $root . $params ['merId'] . '_' . $params ['batchNo'] . '_' . $params ['txnTime'] . 'txt';
            } else {
                $filePath = $root . $params ['fileName'];
            }
            $handle = fopen($filePath, "w+");
            if (!is_writable($filePath)) {
            } else {
                file_put_contents($filePath, $content);
            }
            fclose($handle);
        }
    }
}

/**
 * 构造自动提交表单
 *
 * @param unknown_type $params        	
 * @param unknown_type $action        	
 * @return string
 */
function create_html($params, $action) {
    $encodeType = isset($params ['encoding']) ? $params ['encoding'] : 'UTF-8';
    $html = <<<eot
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset={$encodeType}" />
</head>
<body  onload="javascript:document.pay_form.submit();">
    <form id="pay_form" name="pay_form" action="{$action}" method="post">
	
eot;
    foreach ($params as $key => $value) {
        $html .= "    <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";
    }
    $html .= <<<eot
    <input type="submit" type="hidden">
    </form>
</body>
</html>
eot;
    return $html;
}

?>